package com.treasure.mall.zbqmallmanager.controller.live;

import com.treasure.mall.biz.dto.condition.LiveRoomVideoSearchCondition;
import com.treasure.mall.biz.dto.live.LiveRoomImagesSaveDTO;
import com.treasure.mall.biz.entity.live.LiveRoomImagesPO;
import com.treasure.mall.biz.remote.live.TencentIMService;
import com.treasure.mall.biz.remote.live.dto.GeneralMessageDTO;
import com.treasure.mall.biz.remote.live.enums.MessageType;
import com.treasure.mall.biz.service.core.live.LiveRoomImagesService;
import com.treasure.mall.biz.service.core.live.LiveRoomService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallmanager.controller.live
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/27 2:26 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Controller
@RequestMapping("/live")
public class LiveImagesController {

    @Autowired
    private LiveRoomService liveRoomService;

    @Autowired
    private LiveRoomImagesService liveRoomImagesService;
    @Autowired
    private TencentIMService tencentIMService;

    /**
     * 直播间视频列表
     *
     * @param roomId
     * @param pageIndex
     * @param model
     * @return
     */
    @GetMapping("/detail/images")
    public String videos(
            @RequestParam(required = false, defaultValue = "0") Long roomId,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        model.addAttribute("room", liveRoomService.findById(roomId));
        LiveRoomVideoSearchCondition condition = new LiveRoomVideoSearchCondition();
        condition.setRoomId(roomId);
        Page<LiveRoomImagesPO> pos = liveRoomImagesService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", pos.getTotalElements());
        model.addAttribute("totalPages", pos.getTotalPages());
        model.addAttribute("list", pos.getContent());
        return "live/live_detail_images";
    }

    /**
     * 添加直播间视频
     *
     * @param dto
     * @return
     */
    @PostMapping("/detail/images/sendVideoMessage")
    @ResponseBody
    public ApiResult sendVideoMessage(LiveRoomImagesSaveDTO dto) {
        liveRoomImagesService.save(dto);
        return ApiResult.ok();
    }

    /**
     * 删除直播间视频
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/images/delete")
    @ResponseBody
    public ApiResult deleteVideo(@RequestParam Long id) {
        LiveRoomImagesPO po = liveRoomImagesService.findById(id);
        if (po != null) {
            liveRoomImagesService.delete(id);
            //发送关闭消息
            tencentIMService.sendMessage(GeneralMessageDTO.builder()
                    .roomId(po.getRoomId())
                    .build(), MessageType.closeImages);

            return ApiResult.ok();
        } else {
            return ApiResult.error("图片不存在或已删除");
        }
    }

    /**
     * 关闭正在播放的视频
     *
     * @param id 视频ID
     * @return
     */
    @GetMapping("/detail/images/close")
    @ResponseBody
    public ApiResult closeVideo(@RequestParam Long id) {
        liveRoomImagesService.close(id);
        return ApiResult.ok();
    }


    /**
     * 推送直播间视频
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/images/push")
    @ResponseBody
    public ApiResult pushVideo(@RequestParam Long id) {
        LiveRoomImagesPO po = liveRoomImagesService.findById(id);
        if (po != null) {
            liveRoomImagesService.resetPushed(po.getRoomId());
        }
        liveRoomImagesService.push(id);
        return ApiResult.ok();
    }

}
